﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace Test02
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("钟水桥20217141的组合模式");
            //客户端最终输出的图形
            Graphics graphics = new CompleGraphics("一个复合图形加两个线段组成");
            //添加第一个线段
            graphics.Add(new Line("线段A"));
            //添加第二个线段
            Graphics l = new Line("线段B");
            graphics.Add(l);
            //添加第三个复合图形
            CompleGraphics compleGraphics = new CompleGraphics("一个圆加两个线段");
            compleGraphics.Add(new Cirle("圆C"));
            graphics.Add(new Line("线段D"));
            graphics.Add(new Line("线段E"));

            graphics.Add(compleGraphics);
            //绘制图形
            Console.WriteLine("一个复合图形加两个线段组成");
            graphics.Draw();
            Console.WriteLine("绘制完成");

            //移除线段B 重新绘制
            Console.WriteLine("移除线段B 重新绘制");
            graphics.Remove(l);
            graphics.Draw();
            Console.ReadKey();
        }
    }

    abstract class Graphics //抽象构件 图形类
    {
        private String name;

        public string Name { get => name; set => name = value; }

        public Graphics( String name) { 
            this.name = name;
        }

        //提供统一处理方法
        public abstract void Draw();
        public abstract void Add(Graphics g);

        public abstract void Remove(Graphics g);
    }
    class Line : Graphics //线段基本图形
    {
        public Line(string name) : base(name)
        {
        }

        public override void Add(Graphics g)
        {
            throw new NotImplementedException();
        }

        public override void Draw()
        {
            Console.WriteLine("绘制一个图形" + Name);
        }

        public override void Remove(Graphics g)
        {
            throw new NotImplementedException();
        }
    }
    class Cirle : Graphics //圆 基本图形
    {
        public Cirle(string name) : base(name)
        {
        }

        public override void Add(Graphics g)
        {
            throw new NotImplementedException();
        }

        public override void Draw()
        {
            Console.WriteLine("绘制一个图形" + Name);
        }

        public override void Remove(Graphics g)
        {
            throw new NotImplementedException();
        }
    }
    class CompleGraphics : Graphics //复合图形
    {
        public CompleGraphics(string name) : base(name)
        {
        }

        //使用集合数据保存所有图形
        List<Graphics> graphicsList = new List<Graphics>();
        public override void Add(Graphics g)
        {
            graphicsList.Add(g);
        }

        public override void Draw()
        {
            // Console.WriteLine("绘制一个图形" + Name);

            foreach (var item in graphicsList) {
                item.Draw();
            }
        }

        public override void Remove(Graphics g)
        {
            graphicsList.Remove(g);
        }
    }
}
